package com.cskt.itripbiz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskt.common.condition.AddHotelOrderCondition;
import com.cskt.common.vo.StoreVO;
import com.cskt.entity.HotelTempStore;
import com.cskt.itripbiz.service.HotelTempStoreService;
import com.cskt.mapper.HotelTempStoreMapper;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HotelTempStoreServiceImpl extends ServiceImpl<HotelTempStoreMapper, HotelTempStore> implements HotelTempStoreService {
    @Override
    public Boolean validateRoomStore(AddHotelOrderCondition condition) {
        Map<String,Object> paramMap = conditionToParamMap(condition);
        //执行房间库存更新
        // baseMapper.flushStore(paramMap);
        //查询当前库存

        List<StoreVO> storeVOList = baseMapper.selectStoreVOByCondition(condition);

        if (CollectionUtils.isEmpty(storeVOList)){
            return false;
        }

        for (StoreVO vo : storeVOList) {
            if (vo.getStore() < condition.getCount()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 取出mysql数据库中定义的函数所需要的参数并封装到Map中
     *
     * @param condition
     * @return
     */
    private Map<String,Object> conditionToParamMap(AddHotelOrderCondition condition){
        Map<String,Object> paramMap = new HashMap<>(16);
        paramMap.put("roomId",condition.getRoomId());
        paramMap.put("hotelId", condition.getHotelId());
        paramMap.put("checkInDate", condition.getCheckInDate());
        paramMap.put("checkOutDate", condition.getCheckOutDate());
        return paramMap;
    }
}
